import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SpringLayout;

/**
 * Clase que se encarga crear la interfaz cientifica asi como de manejar los eventos
 */
public class InterfazCientifica extends Interfaz implements ActionListener
{
    /**
     * Campo de atributos privados
     */
	private String cientificaString[] = {"ln","logn","tan","cos","sin",
                                            "x^2","x^y","x^3","n!","log",
                                            "pi","x10","root","sqrt","mod"}; 
        String modo =  "dec";
	private JPanel cient;
	private JButton cientBots[];
	private JRadioButton dec, bin, hex, oct;
	private JPanel radioPanel;
	
        /**
         * Constructor que crea la interfaz cientifica y llama al metodo createMenuBar para crear el JMenuBar
         * @param  titulo es el titulo a usar por el JFrame
         */
	public InterfazCientifica(String titulo)
	{
		super(titulo);
		
		//SpringLayout lay = new SpringLayout();
		setLayout(new BorderLayout());
		text = new JTextArea(3, 25);
		JScrollPane scroll = new JScrollPane(text);
		Font bigFont = new Font("Serif", Font.PLAIN, 20);
		text.setFont(bigFont);
		getContentPane().add(scroll, BorderLayout.PAGE_START);
		
		ButtonGroup group = new ButtonGroup();
		radioPanel = new JPanel(new FlowLayout());

		/* RadioButtons */
	    dec = new JRadioButton("Decimal");
	    dec.setSelected(true);
            dec.addActionListener(this);
	    group.add(dec);
	    radioPanel.add(dec);
	    
	    bin = new JRadioButton("Binario");
            bin.addActionListener(this);
	    group.add(bin);
	    radioPanel.add(bin);
	    
	    hex = new JRadioButton("Hex");
            hex.addActionListener(this);
	    group.add(hex);
	    radioPanel.add(hex);
            
            oct = new JRadioButton("Octal");
            oct.addActionListener(this);
            group.add(oct);
            radioPanel.add(oct);
		
		crearNormal();
		
		SpringLayout layB = new SpringLayout();
		cient = new JPanel(layB);
		cientBots = new JButton[cientificaString.length];
		for(int i=0;i<cientificaString.length;i++)
		{
			cientBots[i] = new JButton(cientificaString[i]);
                        cientBots[i].addActionListener(this);
			cient.add(cientBots[i]);
		}
		SpringUtilities.makeCompactGrid(cient, 3, 5, 0, 7, 5, 5);
                
                JPanel izq = new JPanel(new BorderLayout());
                izq.add(radioPanel, BorderLayout.PAGE_START);
                izq.add(cient, BorderLayout.CENTER);
			
		getContentPane().add(izq, BorderLayout.LINE_START);
		getContentPane().add(butonPanel, BorderLayout.CENTER);
			
		setJMenuBar(createMenuBar(2));	
		normal.addActionListener(this);
		cientifica.addActionListener(this);
		pack();
	}
        /**
         * Metodo que se encarga de manejar los eventos
         * @param  e Encapsula entre otras cosas el objeto que activo el evento
         */
	public void actionPerformed(ActionEvent e)
	{
         
		super.actionPerformed(e);
                if(e.getSource()==bin || e.getSource()==dec || e.getSource()==oct || e.getSource()==hex  )
                {
                if(text.getText() != "")
                {
                    if(dec.isSelected())
                    {
                        try
                        {
                            if(modo == "bin")
                            {
                                String num = text.getText();
                                int numero = Integer.parseInt(num, 2);
                                num = Integer.toString(numero);
                                text.setText(num);
                                modo = "dec";
                            }
                            else if(modo == "hex")
                            {
                                String num = text.getText();
                                int numero = Integer.parseInt(num, 16);
                                num = Integer.toString(numero);
                                text.setText(num);
                                modo = "dec";
                            }
                            else if(modo == "oct")
                            {
                                String num = text.getText();
                                int numero = Integer.parseInt(num, 8);
                                num = Integer.toString(numero);
                                text.setText(num);
                                modo = "dec";
                            }
                        }
                        catch(NumberFormatException ex)
                        {
                            
                        }
                    }
                    else if(bin.isSelected())
                    {
                        try
                        {
                            if(modo == "dec")
                            {
                                String num = text.getText();
                                int numero = Integer.parseInt(num);
                                num = Integer.toBinaryString(numero);
                                text.setText(num);
                                modo = "bin";
                            }
                            else if(modo == "oct")
                            {
                                String num = text.getText();
                                int numero = Integer.parseInt(num, 8);
                                num = Integer.toBinaryString(numero);
                                text.setText(num);
                                modo = "bin";
                            }
                            else if(modo == "hex")
                            {
                                String num = text.getText();
                                int numero = Integer.parseInt(num, 16);
                                num = Integer.toBinaryString(numero);
                                text.setText(num);
                                modo = "bin";
                            }
                        }
                        catch(NumberFormatException ex)
                        {
                            
                        }
                    }
                    else if(hex.isSelected())
                    {
                        try
                        {
                            if(modo == "dec")
                            {
                                String num = text.getText();
                                int numero = Integer.parseInt(num);
                                num = Integer.toHexString(numero);
                                text.setText(num);
                                modo = "hex";
                            }
                            else if(modo == "oct")
                            {
                                String num = text.getText();
                                int numero = Integer.parseInt(num, 8);
                                num = Integer.toHexString(numero);
                                text.setText(num);
                                modo = "hex";
                            }
                            else if(modo == "bin")
                            {
                                String num = text.getText();
                                int numero = Integer.parseInt(num, 2);
                                num = Integer.toHexString(numero);
                                text.setText(num);
                                modo = "hex";
                            }
                        }
                        catch(NumberFormatException ex)
                        {
                            
                        }
                    }
                    else if(oct.isSelected())
                    {
                        try
                        {
                        if(modo == "bin")
                        {
                            String num = text.getText();
                            int numero = Integer.parseInt(num, 2);
                            num = Integer.toOctalString(numero);
                            text.setText(num);
                             modo = "oct";
                        }
                        else if(modo == "dec")
                        {
                            String num = text.getText();
                            int numero = Integer.parseInt(num);
                            num = Integer.toOctalString(numero);
                            text.setText(num);
                             modo = "oct";
                        }
                        else if(modo == "hex")
                        {
                            String num = text.getText();
                            int numero = Integer.parseInt(num, 16);
                            num = Integer.toOctalString(numero);
                            text.setText(num);
                             modo = "oct";
                        }
                         }
                        catch(NumberFormatException ex)
                        {
                            
                        }
                    }
                }
                }
		if(normal.isSelected())
                {
                    System.out.print(normal.isSelected());
                    InterfazNormal nor = new InterfazNormal("Normal");
                    dispose();
                    nor.setVisible(true);
                }
               
                for(int i=0;i<5;i++)
                {
                    if(e.getSource() == cientBots[i])
                    {
                        if(e.getSource()==cientBots[1])
                        {
                            String base = (String)JOptionPane.showInputDialog(
                                        this,
                                        "Base ?",
                                        "Introdusca las base",
                                        JOptionPane.OK_CANCEL_OPTION,
                                        null,
                                        null,
                                        "");
                            
                           //If a string was returned, say so.
                            if ((base != null) && (base.length() > 0)) {
                                text.setText( text.getText() + "log"+base+"(");
                            }
                        }
                        else
                            text.setText( text.getText() + cientBots[i].getText()+'(');
                    }
                }
                
                if(e.getSource()==cientBots[5])
                {
                    text.setText( text.getText() + "^2");
                }
                else if(e.getSource()==cientBots[6])
                {
                    text.setText( text.getText() + "^");
                }
                else if(e.getSource()==cientBots[7])
                {
                    text.setText( text.getText() + "^3");
                }
                else if(e.getSource()==cientBots[8])
                {
                    text.setText( text.getText() + "!");
                }
                else if(e.getSource()==cientBots[9])
                {
                     text.setText( text.getText() + "log10(");
                }
                else if(e.getSource()==cientBots[10])
                {
                    text.setText( text.getText() + "3,14159265");
                }
                else if(e.getSource()==cientBots[11])
                {
                    text.setText( text.getText() + "x10");
                }
                else if(e.getSource()==cientBots[12])
                {
                    text.setText( text.getText() + "root(");    //ej. 3root(3*2) raiz cubica de 3*2
                }
                else if(e.getSource()==cientBots[13])
                {
                    text.setText( text.getText() + "sqrt(");
                }
                else if(e.getSource()==cientBots[14])
                {
                    text.setText( text.getText() + "mod");
                }
	}

}
